
package io.jpress.module.findofbeauty.controller;



import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import io.jboot.utils.CookieUtil;
import io.jboot.web.controller.annotation.RequestMapping;

import io.jpress.JPressConsts;

import io.jpress.commons.layer.SortKit;
import io.jpress.core.menu.annotation.AdminMenu;

import io.jpress.core.template.Template;
import io.jpress.core.template.TemplateManager;
import io.jpress.module.article.ArticleFields;
import io.jpress.module.article.model.Article;
import io.jpress.module.article.model.ArticleCategory;
import io.jpress.module.article.service.ArticleCategoryService;
import io.jpress.module.article.service.ArticleCommentService;
import io.jpress.module.article.service.ArticleService;
import io.jpress.web.base.AdminControllerBase;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.List;



@RequestMapping(value = "/admin/findofbeauty", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _FindOfBeautyController extends AdminControllerBase {

    @Inject
    private ArticleService articleService;
    @Inject
    private ArticleCategoryService categoryService;
    @Inject
    private ArticleCommentService commentService;

    @AdminMenu(text = "动态", groupId = "findofbeauty", order = 1)
    public void index() {
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 42L; // getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    @AdminMenu(text = "\"最IN\"主题活动", groupId = "findofbeauty", order = 2)
    public void themeActivities() {
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 43L; // getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    @AdminMenu(text = "\"最IN\"微视频大赛", groupId = "findofbeauty", order = 3)
    public void game(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 44L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    @AdminMenu(text = "最美景观线（景点）", groupId = "findofbeauty", order = 4)
    public void scenic(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 45L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);

    }

    @AdminMenu(text = "最美车行道", groupId = "findofbeauty", order = 5)
    public void carRoad(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 47L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    @AdminMenu(text = "最美骑行道", groupId = "findofbeauty", order = 6)
    public void bicycleRoad(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 48L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    @AdminMenu(text = "最美登山道", groupId = "findofbeauty", order = 7)
    public void ascend(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 49L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    /*@AdminMenu(text = "最美餐饮", groupId = "findofbeauty", order = 8)
    public void cook(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 50L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }
*/
    @AdminMenu(text = "最美旅游体验点", groupId = "findofbeauty", order = 9)
    public void experience(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 50L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }
    @AdminMenu(text = "最美旅游游览点", groupId = "findofbeauty", order = 10)
    public void experience1(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 51L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    @AdminMenu(text = "最美游客中心", groupId = "findofbeauty", order = 11)
    public void visitorCenter(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 52L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
        renderOkJson();
    }

    @AdminMenu(text = "抖音线路", groupId = "findofbeauty", order = 12)
    public void douyinLine(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = 53L; //getParaToLong("categoryId");
        findAll(status, title, categoryId);
    }

    public void a(){
        render("custom/sceneryinfo.html");
    }

    public void b(){
        Integer id = Integer.valueOf(getPara("id"));
        setAttr("id",id);
        render("custom/sceneryupdate.html");
    }

    @AdminMenu(text = "写文章", groupId = "findofbeauty", order = 0)
    public void write() {
        String type = "findofbeauty";

        List<ArticleCategory> categories = categoryService.findListByType(type);//(ArticleCategory.TYPE_CATEGORY);
        SortKit.toLayer(categories);
        setAttr("categories", categories);
        setAttr("fields", ArticleFields.me());
        setAttr("attrid", CookieUtil.get(this,"attrid"));


        int articleId = getParaToInt(0, 0);

        Article article = null;
        if (articleId > 0) {
            article = articleService.findById(articleId);
            if (article == null) {
                renderError(404);
                return;
            }
            setAttr("article", article);

            List<ArticleCategory> tags = categoryService.findTagListByArticleId(articleId);
            setAttr("tags", tags);

            Long[] categoryIds = categoryService.findCategoryIdsByArticleId(articleId);
            flagCheck(categories, categoryIds);
        }

        String editMode = article == null ? getCookie(JPressConsts.COOKIE_EDIT_MODE) : article.getEditMode();
        setAttr("editMode", JPressConsts.EDIT_MODE_MARKDOWN.equals(editMode)
                ? JPressConsts.EDIT_MODE_MARKDOWN
                : JPressConsts.EDIT_MODE_HTML);

        initStylesAttr("article_");

        render("article/article_write.html");
    }

    public void write1() {
        String type = "findofbeauty";

        List<ArticleCategory> categories = categoryService.findListByType(type);//(ArticleCategory.TYPE_CATEGORY);
        SortKit.toLayer(categories);
        setAttr("categories", categories);
        setAttr("fields", ArticleFields.me());
        setAttr("attrid", CookieUtil.get(this,"attrid"));


        int articleId = getParaToInt(0, 0);

        Article article = null;
        if (articleId > 0) {
            article = articleService.findById(articleId);
            if (article == null) {
                renderError(404);
                return;
            }
            setAttr("article", article);

            List<ArticleCategory> tags = categoryService.findTagListByArticleId(articleId);
            setAttr("tags", tags);

            Long[] categoryIds = categoryService.findCategoryIdsByArticleId(articleId);
            flagCheck(categories, categoryIds);
        }

        String editMode = article == null ? getCookie(JPressConsts.COOKIE_EDIT_MODE) : article.getEditMode();
        setAttr("editMode", JPressConsts.EDIT_MODE_MARKDOWN.equals(editMode)
                ? JPressConsts.EDIT_MODE_MARKDOWN
                : JPressConsts.EDIT_MODE_HTML);

        initStylesAttr("article_");

        render("article/article_write1.html");
    }

    public void queryByType(){
        String status = getPara("status");
        String title = getPara("title");
        Long categoryId = getParaToLong("categoryId");
        findByType(status, title, categoryId);
    }

    private void initStylesAttr(String prefix) {
        Template template = TemplateManager.me().getCurrentTemplate();
        if (template == null){
            return;
        }
        List<String> styles = template.getSupportStyles(prefix);
        if (styles != null && !styles.isEmpty()) {
            setAttr("styles", styles);
        }
    }

    public void findByType(String status, String title,  Long categoryId){
        Page<Article> page =
                StringUtils.isBlank(status)
                        ? articleService._paginateWithoutTrash(getPagePara(), 10, title, categoryId)
                        : articleService._paginateByStatus(getPagePara(), 10, title, categoryId, status);

        setAttr("page", page);

        int draftCount = articleService.findCountByStatus(Article.STATUS_DRAFT);
        int trashCount = articleService.findCountByStatus(Article.STATUS_TRASH);
        int normalCount = articleService.findCountByStatus(Article.STATUS_NORMAL);
        setAttr("draftCount", draftCount);
        setAttr("trashCount", trashCount);
        setAttr("normalCount", normalCount);
        setAttr("totalCount", draftCount + trashCount + normalCount);

        List<ArticleCategory> categories = categoryService.findListByType(ArticleCategory.TYPE_CATEGORY);
        SortKit.toLayer(categories);
        setAttr("categories", categories);

        flagCheck(categories, categoryId);

        render("custom/findofbeauty_list.html");
    }

    public void findAll(String status, String title,  Long categoryId){
        Page<Article> page =
                StringUtils.isBlank(status)
                        ? articleService._paginateWithoutTrash(getPagePara(), 10, title, categoryId)
                        : articleService._paginateByStatus(getPagePara(), 10, title, categoryId, status);

        setAttr("page", page);

        int draftCount = articleService.findCountByStatus(Article.STATUS_DRAFT);
        int trashCount = articleService.findCountByStatus(Article.STATUS_TRASH);
        int normalCount = articleService.findCountByStatus(Article.STATUS_NORMAL);
        setAttr("draftCount", draftCount);
        setAttr("trashCount", trashCount);
        setAttr("normalCount", normalCount);
        setAttr("totalCount", draftCount + trashCount + normalCount);

        List<ArticleCategory> categories = categoryService.findListByType(ArticleCategory.TYPE_CATEGORY);
        SortKit.toLayer(categories);
        setAttr("categories", categories);

        flagCheck(categories, categoryId);

        render("custom/findof_list.html");
    }

    private void flagCheck(List<ArticleCategory> categories, Long... checkIds) {
        if (checkIds == null || checkIds.length == 0
                || categories == null || categories.size() == 0) {
            return;
        }

        for (ArticleCategory category : categories) {
            for (Long id : checkIds) {
                if (id != null && id.equals(category.getId())) {
                    category.put("isCheck", true);
                }
            }
        }
    }
}
